package com.qianying.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianying.comment.CommonResult;
import com.qianying.entity.ExpenseItem;
import com.qianying.service.ExpenseItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 费用项目控制器
 * 提供费用项目的HTTP接口，处理前端请求
 */
@RestController
@RequestMapping("/admin/expenseItem")
@Tag(name = "费用项目(费用项目管理)", description = "费用项目信息的增删改查接口")
public class ExpenseItemController {

    @Autowired
    private ExpenseItemService expenseItemService;

    /**
     * 分页查询费用项目列表
     * @param current 页码
     * @param size 每页条数
     * @param itemName 费用项目名称（模糊查询）
     * @return 分页结果
     */
    @GetMapping("/getPage")
    @Operation(
            summary = "分页查询费用项目列表",
            description = "支持按费用项目名称模糊查询，返回分页数据",
            parameters = {
                    @Parameter(name = "current", description = "页码", example = "1"),
                    @Parameter(name = "size", description = "每页条数", example = "10"),
                    @Parameter(name = "itemName", description = "费用项目名称（模糊查询）", example = "人工费")
            }
    )
    public CommonResult<IPage<ExpenseItem>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String itemName) {
        Page<ExpenseItem> page = new Page<>(current, size);
        return expenseItemService.getPage(page, itemName);
    }

    /**
     * 根据ID查询费用项目详情
     * @param id 主键ID
     * @return 费用项目详情
     */
    @GetMapping("/getById")
    @Operation(
            summary = "查询费用项目详情",
            description = "通过ID获取费用项目的完整信息",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1")
            }
    )
    public CommonResult<ExpenseItem> getById(@RequestParam Long id) {
        return expenseItemService.getById(id);
    }

    /**
     * 新增费用项目
     * @param expenseItem 费用项目信息（ID需为空）
     * @return 操作结果
     */
    @PostMapping("/saveItem")
    @Operation(
            summary = "新增费用项目",
            description = "创建新的费用项目记录，费用项目名称为必填项，ID必须为空",
            parameters = {
                    @Parameter(name = "itemName", description = "费用项目名称", required = true, example = "人工费"),
                    @Parameter(name = "itemCode", description = "费用项目代号", example = "LABOR"),
                    @Parameter(name = "description", description = "费用项目说明", example = "人工费用，按工时计算")
            }
    )
    public CommonResult<?> saveItem(@RequestBody ExpenseItem expenseItem) {
        return expenseItemService.saveItem(expenseItem);
    }

    /**
     * 更新费用项目
     * @param expenseItem 费用项目信息（需包含ID）
     * @return 操作结果
     */
    @PostMapping("/updateItem")
    @Operation(
            summary = "更新费用项目",
            description = "修改已有费用项目信息，必须包含ID和费用项目名称",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1"),
                    @Parameter(name = "itemName", description = "费用项目名称", required = true, example = "人工费"),
                    @Parameter(name = "itemCode", description = "费用项目代号", example = "LABOR"),
                    @Parameter(name = "description", description = "费用项目说明", example = "人工费用，按工时计算")
            }
    )
    public CommonResult<?> updateItem(@RequestBody ExpenseItem expenseItem) {
        return expenseItemService.updateItem(expenseItem);
    }

    /**
     * 根据ID删除费用项目
     * @param id 主键ID
     * @return 操作结果
     */
    @PostMapping("/removeById")
    @Operation(
            summary = "删除费用项目",
            description = "通过ID删除指定的费用项目记录",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1")
            }
    )
    public CommonResult<?> removeById(@RequestParam Long id) {
        return expenseItemService.removeById(id);
    }

    /**
     * 查询所有费用项目（不分页）
     * @return 费用项目列表
     */
    @GetMapping("/getAll")
    @Operation(
            summary = "查询所有费用项目",
            description = "返回所有费用项目列表，适用于下拉选择等场景"
    )
    public CommonResult<List<ExpenseItem>> getAll() {
        return expenseItemService.getAll();
    }
}
